<template>
  <div class="flex-box" ref="tableContainerRef" style="position: relative;">
    <!-- 查询条件 -->
    <div class="search-box" ref="tableHeaderRef" v-show="props.showForm">
      <div class="operation" v-show="props.showPanel">
        <el-tooltip content="刷新当前页面" placement="top">
          <el-icon class="icon-item" size="20" @click="refreshPage" v-show="props.showSet"><Refresh/></el-icon>
        </el-tooltip>
        <el-tooltip content="设置当前页面表格选项" placement="top">
          <el-icon class="icon-item marginL10" size="20" @click="changeTh" v-show="props.showSet"><Setting/></el-icon>
        </el-tooltip>
      </div>
      <el-form :inline="true" class="demo-form-inline">
        <slot name="form"></slot>
        <el-form-item v-show="props.showFromQuery">
          <el-button icon="Search" type="primary" :disabled="indexStore.loading" @click="getSearchList">查询</el-button>
          <el-button icon="Refresh" type="info" size="small" @click="getResetList">重 置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 数据表格 -->
    <div class="table-box">
      <div class="info-box flexDetween  paddingB10 width100">
        <el-row ref="button" class="search-button">
          <el-col :span="24">
            <slot name="button"></slot>
          </el-col>
        </el-row>
        <div class="icon-box marginR20" v-show="!props.showPanel">
          <el-tooltip content="设置当前页面表格选项" placement="top">
            <el-icon class="icon-item" size="20" @click="changeTh" v-show="props.showSet"><Setting/></el-icon>
          </el-tooltip>
          <el-tooltip content="导出" placement="top">
            <el-icon class="icon-item" size="20" @click="exportDownload" v-show="props.showExport"><Download/></el-icon>
          </el-tooltip>
          <el-tooltip content="刷新当前页面" placement="top">
            <el-icon class="icon-item" size="20" @click="refreshPage" v-show="props.showSet"><Refresh/></el-icon>
          </el-tooltip>
        </div>
      </div>
      <el-table :data="list" v-loading="loading" @sort-change="sortTable" :key='key' class="table-hight" :row-class-name="tableRowClassName"
        style="width: 100%;" :style="{height:data.tableContentHeight+'px'}">
        <template #empty v-show="list && !list.length">
          <div class="noDataAvailable">
            <img src="@/assets/images/nodata.png" alt="" />
            <p>~ 暂无数据 ~</p>
          </div>
        </template>
        <slot name="table"></slot>
      </el-table>
    </div>
    <el-pagination class="marginT5" v-show="!loading && total" v-model:current-page="query.page" :page-size="query.size"
      :page-sizes="[20, 50, 100, 200, 400]" layout="total,sizes, prev, pager, next" background :total="total"
      @current-change="handleCurrentChange" @size-change="handleSizeChange" />
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, toRefs, onMounted, watch, computed  } from 'vue'
import { ElMessage } from 'element-plus'
import { throttle } from '@/utils/index'
import { useStore } from '@/stores/index'
import { Download, Refresh } from '@element-plus/icons-vue';
const indexStore = useStore()

const props = defineProps({
  list: {
    //  table表格数据
    type: Array,
    default: () => {
      return []
    }
  },
  query: {
    // from 查询数据
    type: Object,
    default: () => {
      return {
        page: 1,
        size: 20
      }
    }
  },
  total: {
    //  分页总数
    type: Number,
    default: () => {
      return 0
    }
  },
  showForm: {
    //  控制【查询菜单】是否显示
    type: Boolean,
    default: () => {
      return true
    }
  },
  showFromQuery: {
    //  控制【查询按钮】是否显示
    type: Boolean,
    default: () => {
      return true
    }
  },
  showFromButton: {
    //  控制【自定义按钮】是否显示
    type: Boolean,
    default: () => {
      return true
    }
  },
  showExport: {
    //  控制【导出】是否显示
    type: Boolean,
    default: () => {
      return false
    }
  },
  showSet: {
    //  控制【表格表头】是否显示
    type: Boolean,
    default: () => {
      return true
    }
  },
  showPanel: {
    //  仪表盘
    type: Boolean,
    default: () => {
      return false
    }
  },
})
const data = reactive({
  tableContentHeight: '' as string | number, //表格高度
  query: {
    agent_id: -1,
    operated_id: -1,
  },
})

let loading = ref(false)

watch(
  () => indexStore.loading,
  (val) => {
    loading.value = val
  }
)

onMounted(async () => {
  initTableMaxHeight()
})


const tableContainerRef = ref<any>()
const tableHeaderRef = ref<any>()
const initTableMaxHeight = () => {
  const tableHeight = tableContainerRef.value.offsetHeight
  const tableHeaderHeight = tableHeaderRef.value.offsetHeight
  const tableFooterRefHeight = 32
  const height = tableHeight - tableHeaderHeight - tableFooterRefHeight-60
  data.tableContentHeight = height > 0 ? height : tableHeight + 'px'
  // console.log('表格高度：',tableHeight,'表头高度：',tableHeaderHeight,'表尾巴高度：',tableFooterRefHeight,'表高度：',height)
}

let key = ref();
const emit = defineEmits(['changePage', 'changeSize', 'search', 'changeTh', 'reset', 'sort', 'download','className'])

const handleCurrentChange = (val: number) => {
  // 改变分页页数
  emit('changePage', val)
  key.value = Math.random()
}
const handleSizeChange = (val: number) => {
  // 改变分页条数
  emit('changeSize', val)
  key.value = Math.random()
}

// 商户类别 + 商户
const getSearchList = throttle(() => {
  emit('search')
}, 500)

const getResetList = throttle(() => {
  emit('reset')
}, 500)

const refreshPage = throttle(() => {
  emit('search')
  ElMessage.success('刷新成功')
}, 500)

const tableRowClassName = ({row}:{row:any}) => {
  emit('className',row)
}

const exportDownload = throttle(() => {
  if(!props.list || !props.list.length) return false
  emit('download')
}, 1000)

interface Column {
  order: string,
  prop: string
}

// 排序
const sortTable = (column: Column) => {
  if(column.order == 'descending'){
    column.order = 'desc'
  }else{
    column.order= 'asc'
  }
  emit('sort', column)
}

const changeTh = () => {
  emit('changeTh')
}
</script>
<style scoped lang="scss">
.search-button {
  margin: -5px 0 0px 0;
}
// 设置表头
.operation {
  width: 50px;
  cursor: pointer;
  position: absolute;
  top: 10px;
  right: 15px;
  text-align: center;
}
</style>
